<script>
	import Vue from 'vue'
	var QQMapWX = require('@/utils/qqMap.js');
	export default {
		onLaunch: function() {
			// #ifdef APP-PLUS
			// this.checkByAndroid()
			// #endif
			// 计算头部高度及其他信息
			uni.getSystemInfo({
				success: function(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif
					// 微信小程序版同步处理
					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					// #endif	
				}
			})
			// 检测是否需要热更新小程序
			// #ifdef MP-WEIXIN
			this.isUpdates()
			// #endif
			// 检测是否需要更新APP
			// #ifdef APP-PLUS
			// 锁定屏幕方向
			this.checkUpadtes()
			plus.screen.lockOrientation('portrait-primary'); //锁定
			// #endif

		},
		onShow: function() {
			// 检测城市是否切换
			if (uni.getStorageSync('coordinate')) {
				this.checkAddressChange()
			}else{
				// 暂定去掉
				// uni.getLocation({
				// 	type: 'wgs84',
				// 	success: map => {},
				// 	fail: (e) => {}
				// });
			}
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			// 检测是否需要更新小程序
			isUpdates() {
				// 获取小程序更新机制兼容
				if (wx.canIUse('getUpdateManager')) {
					const updateManager = wx.getUpdateManager()
					updateManager.onCheckForUpdate(function(res) {
						// 请求完新版本信息的回调
						if (res.hasUpdate) {
							updateManager.onUpdateReady(function() {
								wx.showModal({
									title: '更新提示',
									content: '新版本已经准备好，是否重启应用？',
									showCancel: false,
									success: function(res) {
										if (res.confirm) {
											// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
											updateManager.applyUpdate()
										}
									}
								})
							})
							updateManager.onUpdateFailed(function() {
								// 新的版本下载失败
								wx.showModal({
									title: '已经有新版本了哟~',
									content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
								})
							})
						}
					})
				} else {
					// 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
					wx.showModal({
						title: '提示',
						content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
					})
				}
			},
			// 检测是否需要更新app
			checkUpadtes() {
				var that = this
				var updateInfo = uni.getStorageSync('updateInfo') ? uni.getStorageSync('updateInfo') : {}
				this.$request({
					url: '/Upgrade/get_new',
					success: res => {
						if (res.data.status == 1) {
							plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
								var updated = res.data.data
								if (wgtinfo.version != updated.code) {
									updateInfo.update = true
									updateInfo.pkgUrl = plus.os.name === 'iOS' ? updated.iOS : updated.Android;
									updateInfo.notes = JSON.parse(updated.content)
									updateInfo.version = updated.code
									// 比较版本号
									var splitNowVer = wgtinfo.version.split('.')
									var splitResultVer = updated.code.split('.')
									if (splitNowVer[0] < splitResultVer[0]) {
										updateInfo.updateType = 'big'
									} else {
										updateInfo.updateType = 'small'
									}
									uni.setStorageSync('updateInfo', updateInfo)
								} else {
									// 没有更新
									updateInfo.update = false
									uni.setStorageSync('updateInfo', updateInfo)
								}
							});
							uni.hideLoading()
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			/**检查是否打开GPS功能（android）**/
			checkByAndroid() {
				let system = uni.getSystemInfoSync(); // 获取系统信息
				if (system.platform === 'android') { // 判断平台
					var context = plus.android.importClass("android.content.Context");
					var locationManager = plus.android.importClass("android.location.LocationManager");
					var main = plus.android.runtimeMainActivity();
					var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
					if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
						uni.showModal({
							title: '提示',
							content: '为了更准确为您提供商户信息，请打开GPS定位服务功能',
							showCancel: false, // 不显示取消按钮
							success() {
								if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
									var Intent = plus.android.importClass('android.content.Intent');
									var Settings = plus.android.importClass('android.provider.Settings');
									var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
									main.startActivity(intent); // 打开系统设置GPS服务页面
								} else {
									console.log('GPS功能已开启');
								}
							}
						});
					}
				}
			},
			// 检测是否切换城市
			checkAddressChange() {
				var that = this
				var NowCoordinate = uni.getStorageSync('coordinate')
				uni.getLocation({
					type: 'wgs84',
					success: map => {
						var coordinate = {
							"latitude": map.latitude,
							"longitude": map.longitude
						}
						that.setCityName(coordinate)
					},
					fail: (e) => {}
				});
			},
			// 切换地址弹窗
			modal(coordinate, address) {
				uni.setStorage({
					key: 'changeCityFlag',
					data: {
						flag: true,
						location: {
							latitude: coordinate.latitude,
							longitude: coordinate.longitude,
							cityName: address
						}
					}
				}); 
			},
			setCityName(NowCoordinate) {
				var that = this
				// 实例化腾讯地图API核心类
				var qqmapsdk = new QQMapWX({
					key: '66DBZ-Q4NL4-46HUZ-X6SQW-RRTGS-HQF7I' // 必填
				});
				var Coordinate = uni.getStorageSync('coordinate')
				
				//1、获取当前位置坐标
				//2、根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
				qqmapsdk.reverseGeocoder({
					location: {
						latitude: NowCoordinate.latitude,
						longitude: NowCoordinate.longitude
					},
					success: function(addressRes) {
						var log = addressRes.result.address_component.city;
						if (log != Coordinate.cityName) {
							that.modal(NowCoordinate, log)
						}else{
							// 位置没有出市级，则更换一下当前的位置
							var StorageCoo = uni.getStorageSync('coordinate')
							StorageCoo.latitude = NowCoordinate.latitude
							StorageCoo.longitude = NowCoordinate.longitude
							uni.setStorageSync('coordinate',StorageCoo)
						}
					}
				})
			}
		}
	}
</script>

<style>
	/* colorUI库 */
	@import "/common/main.css";
	@import "/common/icon.css";
	@import "/common/app.css";
	/* thorUI库 */
	@import "/static/thorUI/icon.css";
	@import "/static/thorUI/thorui.css";
	/* 动效库 */
	/* @import "/common/animation.css"; */

	.nav-list {
		display: flex;
		flex-wrap: wrap;
		padding: 0px 40upx 0px;
		justify-content: space-between;
	}

	.nav-li {
		padding: 30upx;
		border-radius: 12upx;
		width: 45%;
		margin: 0 2.5% 40upx;
		background-image: url(https://cdn.nlark.com/yuque/0/2019/png/280374/1552996358352-assets/web-upload/cc3b1807-c684-4b83-8f80-80e5b8a6b975.png);
		background-size: cover;
		background-position: center;
		position: relative;
		z-index: 1;
	}

	.nav-li::after {
		content: "";
		position: absolute;
		z-index: -1;
		background-color: inherit;
		width: 100%;
		height: 100%;
		left: 0;
		bottom: -10%;
		border-radius: 10upx;
		opacity: 0.2;
		transform: scale(0.9, 0.9);
	}

	.nav-li.cur {
		color: #fff;
		background: rgb(94, 185, 94);
		box-shadow: 4upx 4upx 6upx rgba(94, 185, 94, 0.4);
	}

	.nav-title {
		font-size: 32upx;
		font-weight: 300;
	}

	.nav-title::first-letter {
		font-size: 40upx;
		margin-right: 4upx;
	}

	.nav-name {
		font-size: 28upx;
		text-transform: Capitalize;
		margin-top: 20upx;
		position: relative;
	}

	.nav-name::before {
		content: "";
		position: absolute;
		display: block;
		width: 40upx;
		height: 6upx;
		background: #fff;
		bottom: 0;
		right: 0;
		opacity: 0.5;
	}

	.nav-name::after {
		content: "";
		position: absolute;
		display: block;
		width: 100upx;
		height: 1px;
		background: #fff;
		bottom: 0;
		right: 40upx;
		opacity: 0.3;
	}

	.nav-name::first-letter {
		font-weight: bold;
		font-size: 36upx;
		margin-right: 1px;
	}

	.nav-li text {
		position: absolute;
		right: 30upx;
		top: 30upx;
		font-size: 52upx;
		width: 60upx;
		height: 60upx;
		text-align: center;
		line-height: 60upx;
	}

	.text-light {
		font-weight: 300;
	}

	@keyframes show {
		0% {
			transform: translateY(-50px);
		}

		60% {
			transform: translateY(40upx);
		}

		100% {
			transform: translateY(0px);
		}
	}

	@-webkit-keyframes show {
		0% {
			transform: translateY(-50px);
		}

		60% {
			transform: translateY(40upx);
		}

		100% {
			transform: translateY(0px);
		}
	}
</style>
